const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html){
  let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
  newContent = newContent.replace(/<(\/)?pre[^>]*>/gi, '');
  // newContent = newContent.replace(/<pre/g,'<pre style="max-width:100vw;word-wrap:break-word;overflow:auto;background-color:#f6f8fa;padding:8px;">');
  return newContent;
}

function formatRichText2(str) {
    const txt =  str.replace(/<pre/g,`<pre style="max-width:100%:
        word-wrap:break-word;overflow:auto;background-color:#f6f8fa;padding:8px;"`).
        replace(/<strong/g,`<strong style="line-height:2.2`).replace(/<a href/g,`<a
        style="color:#0366d6" href`).replace(/<p>/g, `<p style="word-wrap:break-word;
        ">`).replace(/\<span class=\"hljs-string\"\>\'\.\/src\/main\.js\'\</g, `<span
        class="hljs-string">'./src/main.js'</span>`).replace(/&lt;/,`<`).replace(/&gt/
        ,`>`).replace(/<li>/g,`<li style="word-wrap:break-word;">`).replace(/<img/g,
        `<img style="width:100%"`)
    return txt
}

//计算两个时间之间的时间差 多少天时分秒
function intervalTime(startTime,endTime) {
  // var timestamp=new Date().getTime(); //计算当前时间戳
  var timestamp = (Date.parse(new Date()))/1000;//计算当前时间戳 (毫秒级)
  //  var date1 = ""; //开始时间
  // if(timestamp<startTime){
  //     date1=startTime;
  // }else{
  //     date1 = timestamp; //开始时间
  // }
  // var date2 = endTime; //结束时间
  // var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
  // var date3 =  (date2- date1)*1000; //时间差的毫秒数
  var date2 = new Date(startTime)
  console.log(date2);
  var dt = date2.getTime() * 1000
  var date3 =  (timestamp- dt)*1000; //时间差的毫秒数
  //计算出相差天数
  var days = Math.floor(date3 / (24 * 3600 * 1000));
  //计算出小时数

  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  //计算相差秒数

  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  console.log(days + "天 " + hours + "小时 ")
  // return   days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"
  return  days + "天 " + hours + "小时 " + minutes + " 分钟"
  // return   days + "天 " + hours + "小时 "
  // return '123'
}



module.exports = {
  formatTime,
  formatRichText,
  formatRichText2,
  intervalTime
}
